Motivation

I found a problem with the previous code: I added info on butterfly_flight to the data before calculating the distances and that makes the paths larger than they actually are. This version does not have that problem.

I also changed the code in order to use purrr, a package for doing functional programming. Basically, it makes it way easier to maintain the code by making it shorter.

I suppose that’s enough rambling from me, let’s give you the tables, that’s what you want after all 😄

Data processing

Loads and installs packages used if needed

list_of_packages <- c("tidyverse", "here", "fs", "plotly", "DT")
new_packages <-
  list_of_packages[!(list_of_packages %in% installed.packages()[, "Package"])]
if (length(new_packages))
  install.packages(new_packages)

library(tidyverse)
library(here)
library(fs)
library(plotly)
library(DT)

Loading files

fs::dir_ls(here::here('wand'), recurse = T, regexp = "unpaired-points-xyz.csv$") %>%
  purrr::map_dfr(readr::read_csv, .id = "source") -> all_flights

Measuring flight lenght in frames

all_flights %>%
  dplyr::group_by(source) %>% 
  dplyr::mutate(row_n = row_number()) %>% 
  dplyr::ungroup() %>%
  tidyr::drop_na() %>% 
  dplyr::group_by(source) %>% 
  dplyr::summarise(flight_duration_frames = max(row_n) - min(row_n))
## # A tibble: 35 x 2
##    source                                                   flight_duration_fra…
##    <chr>                                                                   <int>
##  1 /home/francisko/coding/r/flight-path-experiment/wand/b1…                  177
##  2 /home/francisko/coding/r/flight-path-experiment/wand/b1…                  101
##  3 /home/francisko/coding/r/flight-path-experiment/wand/b1…                  152
##  4 /home/francisko/coding/r/flight-path-experiment/wand/b1…                  156
##  5 /home/francisko/coding/r/flight-path-experiment/wand/b1…                  185
##  6 /home/francisko/coding/r/flight-path-experiment/wand/b2…                  198
##  7 /home/francisko/coding/r/flight-path-experiment/wand/b2…                  211
##  8 /home/francisko/coding/r/flight-path-experiment/wand/b2…                  151
##  9 /home/francisko/coding/r/flight-path-experiment/wand/b2…                  147
## 10 /home/francisko/coding/r/flight-path-experiment/wand/b2…                   89
## # … with 25 more rows

Removing NaNs

all_flights %>% 
  tidyr::drop_na() -> all_flights

Defining functions

calc_flight_dist <- function(dat) {
  dat_dist <-  {{dat}} %>% 
  dist() %>%  # calculates distance
  as.matrix()  # converts it to a matrix

return(sum((dat_dist[row(dat_dist) == col(dat_dist) + 1])))  # sums the distance between points by summing the points in line n and column n + 1
  
}
plots_flight_path <- function(dat) {
  
plotly::plot_ly(data = dat,
        type = "scatter3d", 
        x = dat$x_1 ,
        y = dat$y_1,
        z = dat$z_1,
        mode = "lines")
}
calc_straight_dist <- function(dat) {
  
dist_dat <- 
  dat %>% 
  # dplyr::filter(!is.na(x_1)) %>%
  dist() %>% 
  as.matrix()

return(dist_dat[1, nrow(dist_dat)])  
}
all_flights %>%
  dplyr::group_by(source) %>%
  tidyr::nest() %>%
  dplyr::mutate(
    distance = purrr::map_dbl(data, ~ calc_flight_dist(.x)),
    straight_distance = purrr::map_dbl(data, ~ calc_straight_dist(.x)),
    sinuosity = distance / straight_distance,
    plot_3d = purrr::map(data, ~ plots_flight_path(.x)),
    butterfly = stringr::str_extract(source, pattern = "b\\d{3}"),
    take = stringr::str_extract(source, pattern = "t\\d{1}")
  ) %>%
  dplyr::ungroup() -> processed

Here’s the table

processed %>%  
  dplyr::select(butterfly, take, everything(), -source, -data, -plot_3d) %>% 
  DT::datatable(extensions = 'Buttons',
                options = list(dom = 'Blfrtip',
                               buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
                               lengthMenu = list(c(10,25,50,-1),
                                                 c(10,25,50,"All"))))